package com.example.hospital.controller;

import com.example.hospital.dto.QueueQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Queue;
import com.example.hospital.service.QueueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("queue")
@Tag(name = "Queue", description = "排队管理相关接口")
public class QueueController {
    @Resource
    private QueueService queueService;

    /**
     * 新增排队信息
     *
     * @param queue
     * @return queue id or error
     */
    @Operation(
            summary = "新增排队信息",
            description = "创建一个新的排队记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回排队ID"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addQueue(@RequestBody Queue queue) {
        boolean saved = queueService.save(queue);
        return saved ? Result.success("添加成功", queue.getQueueId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除排队信息
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除排队信息",
            description = "通过排队ID删除指定的排队记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "排队记录未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteQueue(@PathVariable("id") Integer id) {
        boolean removed = queueService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改排队信息
     *
     * @param queue
     * @return
     */
    @Operation(
            summary = "修改排队信息",
            description = "通过传递新的排队信息更新现有排队记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updateQueue(@RequestBody Queue queue) {
        boolean updated = queueService.updateById(queue);
        return updated ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id查询排队信息
     *
     * @param id
     * @return queue or error
     */
    @Operation(
            summary = "根据id查询排队信息",
            description = "根据排队ID查询排队信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "排队记录未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Queue> getQueue(@PathVariable("id") Integer id) {
        Queue queue = queueService.getById(id);
        return queue != null ? Result.success("查询成功", queue) : Result.fail("查询失败");
    }

    /**
     * 查询所有排队信息
     *
     * @return queue list
     */
    @Operation(
            summary = "查询所有排队信息",
            description = "获取所有排队记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @GetMapping
    public Result listQueues() {
        return Result.success(queueService.list());
    }

    /**
     * 根据条件查询排队信息
     *
     * @param queueQuery
     * @return queue list
     */
    @Operation(
            summary = "根据条件查询排队信息",
            description = "根据条件查询排队记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "500", description = "服务器错误")
            }
    )
    @PostMapping("/query")
    public Result listQueues(@RequestBody QueueQuery queueQuery) {
        List<Queue> queues = queueService.listQueues(queueQuery);
        return Result.success(queues);
    }

    /**
     * 根据患者id查询排队信息
     *
     * @param patientId
     * @return queue list
     */
    @Operation(
            summary = "根据患者id查询排队信息",
            description = "通过患者ID查询排队信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "患者未找到")
            }
    )
    @GetMapping("/patient/{patientId}")
    public Result<List<Queue>> listQueuesByPatientId(@PathVariable("patientId") Integer patientId) {
        List<Queue> queues = queueService.listQueuesByPatientId(patientId);
        return queues != null ? Result.success("查询成功", queues) : Result.fail("查询失败");
    }

}
